using System;
using System.Collections.Generic;
using System.Text;
using SharpMud.MudLib.Common.Entities;

namespace SharpMud.MudLib.Common.Entities.Players.Persistance
{
    public interface IPlayerCharacterLoadResults<TPlayerInstance> : IPlayerCharacterLoadResults where TPlayerInstance : IPlayerCharacter, new()
    {
        new TPlayerInstance LoadedPlayer { get;}
    }
    public class PlayerLoadResults<TPlayerInstance> : IPlayerCharacterLoadResults<TPlayerInstance> where TPlayerInstance : IPlayerCharacter, new()
    {
        private TPlayerInstance _LoadedPlayer;
        private IPlayerCharacterLoadMessage[]  _PlayerLoadMessages;

        private PlayerLoadResults()
        {
        }
        public PlayerLoadResults(TPlayerInstance loadedPlayer, params IPlayerCharacterLoadMessage[] playerLoadMessages)
        {
            _LoadedPlayer = loadedPlayer;
            _PlayerLoadMessages = playerLoadMessages;
        }

        #region IPlayerCharacterLoadResults<TPlayerInstance> Members

        public TPlayerInstance LoadedPlayer
        {
            get { return _LoadedPlayer; }
        }
        TPlayerInstance IPlayerCharacterLoadResults<TPlayerInstance>.LoadedPlayer
        {
            get { return _LoadedPlayer; }
        }

        #endregion

        #region IPlayerCharacterLoadResults Members

        public IPlayerCharacterLoadMessage[] Messages
        {
            get { return _PlayerLoadMessages; }
        }

        IPlayerCharacter IPlayerCharacterLoadResults.LoadedPlayer
        {
            get
            {
                return (IPlayerCharacter)_LoadedPlayer;
            }
        }

        #endregion
    }

    public interface IPlayerCharacterLoadResults
    {
        IPlayerCharacterLoadMessage[]  Messages        { get;}
        IPlayerCharacter               LoadedPlayer      { get;}
    }
    public interface IPlayerCharacterLoadMessage
    {
        string FileName { get;}
        int Row { get;}
        int Column { get;}
        string Message { get;}
        bool IsCritical { get;}
    }
    public class PlayerLoadMessage : IPlayerCharacterLoadMessage
    {
        private System.Exception                        _InnerException;
        private System.CodeDom.Compiler.CompilerError   _InnerCompilerError;

        private string _FileName;
        private int _Row;
        private int _Column;
        private string _Message;
        private bool _IsCritical;

        private PlayerLoadMessage()
        {
        }

        public PlayerLoadMessage(string message, string fileName, int row, int column, bool isCritical)
        {
            _Message = message;
            _FileName = fileName;
            _Row = row;
            _Column = column;
            _IsCritical = isCritical;
        }

        #region IPlayerCharacterLoadMessage Members

        public string FileName
        {
            get { return _FileName; }
        }

        public int Row
        {
            get { return _Row; }
        }

        public int Column
        {
            get { return _Column; }
        }

        public string Message
        {
            get { return _Message; }
        }

        public bool IsCritical
        {
            get { return _IsCritical; }
        }

        #endregion

        public static IPlayerCharacterLoadMessage FromException(System.Exception exception)
        {
            PlayerLoadMessage alm = new PlayerLoadMessage(exception.Message, exception.Source, -1, -1, true);
            alm._InnerException = exception;
            return alm;
        }

        public static IPlayerCharacterLoadMessage FromCompilerError(System.CodeDom.Compiler.CompilerError compilerError)
        {
            PlayerLoadMessage alm = new PlayerLoadMessage(compilerError.ErrorText, compilerError.FileName, compilerError.Line, compilerError.Column, !compilerError.IsWarning);
            alm._InnerCompilerError = compilerError;
            return alm;
        }
    }
}
